import { service, crudParamsSerializer } from '@/utils/request'
import { CreateQueryParams } from '@nestjsx/crud-request'
import { PlayList } from './play-list.dto'
import { Song } from './song.dto'
import { DeepPartial } from 'ts-essentials'

export const defaultPlayListData: Partial<PlayList> = {}

export function getPlayLists (params?: CreateQueryParams) {
  return service.get<PlayList[]>(
    '/resources/play-lists',
    {
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}

export function getPlayListById (id: number | string, params?: CreateQueryParams) {
  return service.get<PlayList>(
    `/resources/play-lists/${id}`,
    {
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}

export function createPlayList (dto: DeepPartial<Exclude<PlayList, 'id'>>, params?: CreateQueryParams) {
  return service.post<PlayList>(
    '/resources/play-lists',
    dto,
    {
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}

export function removePlayList (id: number | string) {
  return service.delete(
    `/resources/play-lists/${id}`
  )
}

export function getSongsOfPlayList (playListId: number | string) {
  return service.get<Song[]>(
    `/resources/play-lists/${playListId}/songs`
  )
}

export function addSongToPlayList (playListId: number | string, songId: number | string) {
  return service.post<Song>(
    `/resources/play-lists/${playListId}/songs`,
    { songId }
  )
}

export function removeSongFromPlayList (playListId: number | string, songId: number | string) {
  return service.delete<void>(
    `/resources/play-lists/${playListId}/songs/${songId}`
  )
}
